package com.yuju.controller.hotel;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.GoodReturnCodeEnum;
import com.yuju.hotel.model.GoodModel;
import com.yuju.hotel.service.IGoodService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.GoodVO;

/**
 * 
 * 类名：GoodController  <br />
 *
 * 功能：商品相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月30日 上午9:00:34  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("good")
public class GoodController extends BaseController<GoodVO, GoodModel> {

	@Autowired
	private IGoodService goodService;
	
	/**
	 * 
	 * 功能：商品列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:06 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = goodService.pageList(convert(goodVO), goodVO.getPage(), goodVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：新增商品<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:32 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "save", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodService.save(convert(goodVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_SAVE_SUCCESS.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_SAVE_FAILURE.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：更新商品<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:03:48 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "update", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodService.updateSelective(convert(goodVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_UPDATE_FAILURE.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：删除商品<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:04:02 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "del", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		int rows = goodService.del(goodVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_DEL_SUCCESS.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(GoodReturnCodeEnum.GOOD_DEL_FAILURE.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：根据ID获取商品信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月30日 上午9:04:15 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "get", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		GoodModel GoodModel = goodService.findById(goodVO.getId());
		if (Utils.isNotNull(GoodModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(GoodModel);
			result.setReturnCode(GoodReturnCodeEnum.GOOD_QUERY_SUCCESS.getCode());
			result.setReturnMsg(GoodReturnCodeEnum.GOOD_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午10:23:51 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		GoodVO  goodVO = obj.toJavaObject(GoodVO.class);
		Map<String, String> fail = FieldValidator.validateAll(goodVO, "selectbyname", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		boolean selectByName = goodService.selectByName(convert(goodVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
}
